package com.bignerdranch.android.ecar.Activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bignerdranch.android.ecar.R;
import com.bignerdranch.android.ecar.Utils.URLBank;
import com.facebook.drawee.view.SimpleDraweeView;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.HttpMethod;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;

public class UserInfoActivity extends BaseActivity implements View.OnClickListener {
    private ImageButton userinfo_back;
    private SimpleDraweeView head;
    private RelativeLayout part_1;
    private TextView name_text;
    private RelativeLayout part_2;
    private TextView phone_text;
    private RelativeLayout part_3;
    private Button quit;
    SharedPreferences sp;
    SharedPreferences.Editor editor;
    private Uri mUri;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        initView();
        sp = getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
    }

    @Override
    protected void onResume() {
        super.onResume();
        String img_name = sp.getString("headPic",null);
        String url = URLBank.teacher_url+URLBank.upload_url+img_name;
//            Log.e("====url=",url);
        Uri uri = Uri.parse(url);
        head.setImageURI(uri);
    }

    private void initView() {
        userinfo_back = (ImageButton) findViewById(R.id.userinfo_back);
        head = (SimpleDraweeView) findViewById(R.id.head);
        part_1 = (RelativeLayout) findViewById(R.id.part_1);
        part_1.setOnClickListener(this);
        name_text = (TextView) findViewById(R.id.name_text);
        part_2 = (RelativeLayout) findViewById(R.id.part_2);
        part_2.setOnClickListener(this);
        phone_text = (TextView) findViewById(R.id.phone_text);
        part_3 = (RelativeLayout) findViewById(R.id.part_3);
        part_3.setOnClickListener(this);
        quit = (Button) findViewById(R.id.quit);

        userinfo_back.setOnClickListener(this);
        quit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.userinfo_back:
                finish();
                break;
            case R.id.quit:
                editor.clear();
                editor.commit();
                finish();
                break;
            case R.id.part_1:
               /* Intent intent = new Intent(this, PhotoActivity.class);
                startActivity(intent);*/
/*
                PhotoPicker.builder()
                        .setPhotoCount(1)
                        .start(this);*/

                Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, 0);
                break;
            case R.id.part_2:

                break;
            case R.id.part_3:

                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 0) {
            if (resultCode == Activity.RESULT_OK) {
                mUri = data.getData();

//                Log.e("----------",data.getData().toString());
                Intent intent = new Intent("com.android.camera.action.CROP");
                intent.setDataAndType(data.getData(), "image/*");
                intent.putExtra("scale", true);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, data.getData());
                startActivityForResult(intent, 2);
            }
        }
        if (requestCode == 2) {
            if (resultCode == Activity.RESULT_OK) {
                //上传头像，然后再把sp文件中的headPic对应的值改掉
                Log.e("--------uri--",mUri.toString());
                Log.e("----head-",head.toString());
                head.setImageURI(mUri);
                doUpload();


            }

        }
    }

    private void doUpload(){
        RequestParams params = new RequestParams(URLBank.teacher_url+URLBank.uploadphoto_url);
        Log.e("---url--",URLBank.teacher_url+URLBank.uploadphoto_url);
        params.addBodyParameter("id","52");

            params.addBodyParameter("headPhoto",new File(mUri.getPath()),"image/*");
//            Log.e("-------file--",new File(new URI(mUri.toString()))+"");

        x.http().request(HttpMethod.POST, params, new Callback.CommonCallback<JSONObject>() {
            @Override
            public void onSuccess(JSONObject o) {
                Log.e("====onSuccess",o.toString());
                try {
                    editor.putString("headPic",o.getString("data"));
                    Log.e("====data",o.getString("data"));
                    editor.commit();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable throwable, boolean b) {
                Log.e("====onError",throwable.toString());
            }

            @Override
            public void onCancelled(CancelledException e) {
                Log.e("====onCancelled",""+"onCancelled");
            }

            @Override
            public void onFinished() {
                Log.e("====onFinished",""+"onFinished");
            }
        });
    }


}
